package com.sd.web.im;
import com.sd.common.utils.DateProvider;
import com.sd.common.utils.R;
import com.sd.domain.Assistant;
import com.sd.domain.ImCollect;
import com.sd.service.AssistantService;
import com.sd.service.ImCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

/**
 * 助手
 * @author yexinming
 * @date 2020-07-29
 */
@RestController
@RequestMapping("/assistant")
public class AssistantController {
    /**
     * 日志记录组件
     */
    private static final Logger logger = LoggerFactory.getLogger(AssistantController.class);

    @Autowired
    private AssistantService assistantService;

    @Autowired
    private DateProvider dateProvider;

    @PostMapping("/insert")
    public R save(@RequestParam("phone") String phone,
                  @RequestParam("userName") String userName,
                  @RequestParam("nickName") String nickName,
                  @RequestParam("xxl_sso_sessionid") String xxl_sso_sessionid){
       try {
           Date currentTime = dateProvider.getCurrentTime();
           Assistant assistant = new Assistant();
           assistant.setUserName(userName);
           assistant.setPhone(phone);
           assistant.setNickName(nickName);
           assistant.setPartnerid(xxl_sso_sessionid);
           assistant.setCreateTime(currentTime);
           assistant.setUpdateTime(currentTime);
           assistantService.save(assistant);
           return R.ok();
       }catch (Exception e){
           logger.error("error",e);
           return R.error();
       }
    }

    @GetMapping("/find")
    public R find(@RequestParam("xxl_sso_sessionid") String xxl_sso_sessionid){
        try {
             Assistant assistant = assistantService.findByAuthCode(xxl_sso_sessionid);
             return R.ok().put("assistant",assistant);
        }catch (Exception e){
            logger.error("error",e);
            return R.error();
        }
    }

    @GetMapping("/delete")
    public R delete(
            @RequestParam("id") Long id,
            @RequestParam("xxl_sso_sessionid") String xxl_sso_sessionid){
        try {
            assistantService.removeByAuthCode(xxl_sso_sessionid);
            return R.ok();
        }catch (Exception e){
            logger.error("error",e);
            return R.error();
        }
    }
}
